package com.ruoyi.framework.aspectj;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.eamp.domain.EampReMan;
import com.ruoyi.eamp.domain.EampSrLog;
import com.ruoyi.eamp.domain.EampSrMan;
import com.ruoyi.eamp.domain.vo.EampReManVO;
import com.ruoyi.eamp.mapper.EampSrLogMapper;
import com.ruoyi.eamp.service.IEampSrLogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.time.LocalDate;
import java.time.LocalDateTime;


@Aspect
@Component
public class EampSrAddLogAspect {

    @Autowired
    private IEampSrLogService eampSrLogService;

    @Around("@annotation(com.ruoyi.common.annotation.IEampSrAddLog)")
    public Object men(ProceedingJoinPoint joinPoint) throws Throwable {
// 当前登录用户
        Long userId = SecurityUtils.getUserId();
// 操作时间
        LocalDateTime localDate = LocalDateTime.now();
// 操作方法
        String menthodName = joinPoint.getSignature().getName();
//
        Long srId = 0L;
        Long reId = 0L;

        Object result = joinPoint.proceed();

        Object[] args = joinPoint.getArgs();
        for (Object arg : args) {
            if (arg instanceof EampSrMan){
                srId = ((EampSrMan) arg).getSrId();
            }
        }
        EampSrLog eampSrLog = new EampSrLog(null,userId,localDate,menthodName,srId,null,1L);
        eampSrLogService.insertEampSrLog(eampSrLog);
        System.out.println("打印"+userId+localDate+menthodName+srId+reId);
        return result;
    }
//    @After("@annotation(com.ruoyi.common.annotation.IEampSrAddLog)")
//    public void after(JoinPoint joinPoint) throws IllegalAccessException {
//        Object[] args = joinPoint.getArgs();
//        for (Object eve:args){
//            Class<?> aClass = eve.getClass();
//            Field[] declaredFields = aClass.getDeclaredFields();
//            for (Field every:declaredFields){
//                if(every.getName().equals("reId")){
//                    every.setAccessible(true);
//                    Object o = every.get(aClass);
//                    System.out.println(o);
//                }
//
//            }
//        }
//
//    }
}